﻿using System;
using System.Drawing;
using System.Web.UI;
using Trirand.Web.UI.WebControls;
using Web.Account;

namespace AssetManage.Manage
{
    public partial class Manage : Page
    {
        private static String _expenseDeleteSql = @"DELETE FROM [Public] WHERE [ID] = @ID";

        private static String _expenseInsertSql =
            @"INSERT INTO [Public] ([Time], [Expense], [Item]) VALUES (Now(), @Expense, @Item)";

        private static String _expenseSeleteSql = @"SELECT * FROM [Public] ORDER BY [Time] DESC";

        private static String _expenseUpdateSql =
            @"UPDATE [Public] SET [Expense] = @Expense, [Item] = @Item WHERE [ID] = @ID";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Expense.AjaxCallBackMode == AjaxCallBackMode.None)
                {
                    UserID.DataBind();
                    _expenseDeleteSql = @"DELETE FROM [Public] WHERE [ID] = @ID";
                    _expenseInsertSql =
                        @"INSERT INTO [Public] ([Time], [Expense], [Item]) VALUES (Now(), @Expense, @Item)";
                    _expenseSeleteSql = @"SELECT * FROM [Public] ORDER BY [Time] DESC, [ID] DESC";
                    _expenseUpdateSql = @"UPDATE [Public] SET [Expense] = @Expense, [Item] = @Item WHERE [ID] = @ID";
                }
            }
            Expense_SqlDataSource.DeleteCommand = _expenseDeleteSql;
            Expense_SqlDataSource.InsertCommand = _expenseInsertSql;
            Expense_SqlDataSource.SelectCommand = _expenseSeleteSql;
            Expense_SqlDataSource.UpdateCommand = _expenseUpdateSql;
            var accountBalance = AccountManage.GetBalance(UserID.SelectedValue);
            Balance.ForeColor = Color.FromName(accountBalance >= 0 ? "#3c763d" : "#a94442");
            Balance.Text = Math.Round(accountBalance, 2).ToString("0.00");
        }

        protected void UserID_DataBound(object sender, EventArgs e)
        {
            UserID.SelectedValue = "Public";
        }

        protected void UserID_SelectedIndexChanged(object sender, EventArgs e)
        {
            _expenseDeleteSql = @"DELETE FROM [" + UserID.SelectedValue + @"] WHERE [ID] = @ID";
            _expenseInsertSql = @"INSERT INTO [" + UserID.SelectedValue +
                                @"] ([Time], [Expense], [Item]) VALUES (Now(), @Expsense, @Item)";
            _expenseSeleteSql = @"SELECT * FROM [" + UserID.SelectedValue + @"] ORDER BY [Time] DESC, [ID] DESC";
            _expenseUpdateSql = @"UPDATE [" + UserID.SelectedValue +
                                @"] SET [Expense] = @Expense, [Item] = @Item WHERE [ID] = @ID";
        }
    }
}